home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / dr2d.lha / modulesdr2dutil.c < prev    next >
C/C++ Source or Header  |  1991-11-19  |  4KB  |  208 lines

  1. /*    Routines to convert to and from IEEE single precision
  2.  *    numbers.  These use no floating point operations; rather,
  3.  *    they do bit-twiddling to do their work.
  4.  *
  5.  *    Ross Cunniff, Stylus, Inc. September 22, 1991
  6.  */
  7.  
  8. #include "iffp/obj2d.h"
  9.  
  10. void ieee2flt( long *ieee, Coord *flt )
  11. {
  12. #ifdef FLT_COORD
  13.     *flt = *(Coord *)ieee;
  14. #else
  15.     union {
  16.     long
  17.         Number;
  18.     struct {
  19.         unsigned
  20.         Sign : 1,
  21.         Exp  : 8,
  22.         Mant : 23;
  23.     }   Bits;
  24.     }   IeeeBits;
  25.  
  26.     long
  27.     RealMant,
  28.     RealExp;
  29.  
  30.     IeeeBits.Number = *ieee;
  31.     RealExp = IeeeBits.Bits.Exp;
  32.  
  33.     if( RealExp ) {
  34.     RealExp -= 127;        /* Exponent biased by 127 */
  35.     RealExp += FIXOFFS;     /* FIXOFFS digit accuracy */
  36.     RealExp -= 23;        /* Originally 23 digit accuracy */
  37.  
  38.     if( RealExp < 0 ) {
  39.         RealMant = (0x00800000 | IeeeBits.Bits.Mant) >> -RealExp;
  40.     }
  41.     else {
  42.         RealMant = (0x00800000 | IeeeBits.Bits.Mant) << RealExp;
  43.     }
  44.  
  45.     if( IeeeBits.Bits.Sign ) {
  46.         RealMant = -RealMant;
  47.     }
  48.     }
  49.     else {
  50.     RealMant = 0;
  51.     }
  52.  
  53.     *flt = RealMant;
  54. #endif
  55. }
  56.  
  57.  
  58. void flt2ieee( Coord *flt, long *ieee )
  59. {
  60. #ifdef FLT_COORD
  61.     *ieee = *(long *)flt;
  62. #else
  63.     union {
  64.     long
  65.         Number;
  66.     struct {
  67.         unsigned
  68.         Sign : 1,
  69.         Exp  : 8,
  70.         Mant : 23;
  71.     }   Bits;
  72.     }   IeeeBits;
  73.  
  74.     long
  75.     RealMant,
  76.     RealMask,
  77.     RealExp;
  78.  
  79.     RealMant = *flt;
  80.  
  81.     if( RealMant ) {
  82.     /* Get the sign */
  83.     if( RealMant < 0 ) {
  84.         IeeeBits.Bits.Sign = 1;
  85.         RealMant = -RealMant;
  86.     }
  87.     else {
  88.         IeeeBits.Bits.Sign = 0;
  89.     }
  90.  
  91.     /* Get the exponent */
  92.     for(    RealMask = 0x40000000, RealExp = 31;
  93.         RealMask;
  94.         RealMask >>= 1, RealExp-- )
  95.     {
  96.         if( RealMant & RealMask )    break;
  97.     }
  98.  
  99.     if( RealExp > 24 ) {
  100.         RealMant >>= RealExp - 24;
  101.     }
  102.     else {
  103.         RealMant <<= 24 - RealExp;
  104.     }
  105.     RealExp -= FIXOFFS;
  106.     RealExp += 126;
  107.  
  108.     IeeeBits.Bits.Mant = RealMant & 0x007FFFFF;
  109.     IeeeBits.Bits.Exp = RealExp;
  110.     *ieee = IeeeBits.Number;
  111.     }
  112.     else {
  113.     *ieee = 0;
  114.     }
  115. #endif
  116. }
  117.  
  118. /* Free all memory associated with an object */
  119. void FreeObj( struct Obj2D *Obj )
  120. {
  121.     struct Obj2D
  122.     *Curr, *Next;
  123.  
  124.     switch( Obj->Type ) {
  125.     case ID_CPLY :    case ID_OPLY :
  126.     free( Obj->Data.POLYdata.Coords );
  127.     break;
  128.     case ID_TPTH :
  129.     free( Obj->Data.TPTHdata.String );
  130.     free( Obj->Data.TPTHdata.Path );
  131.     break;
  132.     case ID_STXT :
  133.     free( Obj->Data.STXTdata.String );
  134.     break;
  135.     case ID_GRUP :
  136.     for( Curr = Obj->Data.GRUPdata.Objs; Curr; Curr = Next ) {
  137.         Next = Curr->Next;
  138.         FreeObj( Curr );
  139.     }
  140.     break;
  141.     case ID_XTRN :
  142.     FreeObj( Obj->Data.XTRNdata.Obj );
  143.     break;
  144.     case ID_VBM :
  145.     free( Obj->Data.VBMdata.Path );
  146.     break;
  147.     }
  148.     free( Obj );
  149. }
  150.  
  151.  
  152. /* Free all memory associated with a DR2D file contents */
  153. void FreeConts( struct Proj2D *Conts )
  154. {
  155.     int
  156.     i;
  157.     struct Obj2D
  158.     *Curr, *Next;
  159.  
  160.     if( Conts->MaxFont ) {
  161.     free( Conts->FontTable );
  162.     for( i = 0; i < Conts->MaxFont; i++ ) {
  163.         if( Conts->FontNames[i] )    free( Conts->FontNames[i] );
  164.     }
  165.     free( Conts->FontNames );
  166.     }
  167.  
  168.     if( Conts->MaxDash ) {
  169.     free( Conts->DashCounts );
  170.     for( i = 0; i < Conts->MaxDash; i++ ) {
  171.         if( Conts->DashPatts[i] )    free( Conts->DashPatts[i] );
  172.     }
  173.     free( Conts->DashPatts );
  174.     }
  175.  
  176.     if( Conts->NumColor ) {
  177.     free( Conts->RGB_Table );
  178.     free( Conts->CMYK_Table );
  179.     for( i = 0; i < Conts->NumColor; i++ ) {
  180.         if( Conts->CNAM_Table[i] )    free( Conts->CNAM_Table[i] );
  181.     }
  182.     free( Conts->CNAM_Table );
  183.     }
  184.  
  185.     if( Conts->MaxFills ) {
  186.     for( i = 0; i < Conts->MaxFills; i++ ) {
  187.         if( Conts->FillTable[i] )    FreeObj( Conts->FillTable[i] );
  188.     }
  189.     free( Conts->FillTable );
  190.     }
  191.  
  192.     if( Conts->MaxLayer ) {
  193.     free( Conts->LayerTable );
  194.     free( Conts->LastObjs );
  195.     for( i = 0; i < Conts->MaxLayer; i++ ) {
  196.         for( Curr = Conts->Objects[i]; Curr; Curr = Next ) {
  197.         Next = Curr->Next;
  198.         FreeObj( Curr );
  199.         }
  200.     }
  201.     }
  202.  
  203.     free( Conts );
  204. }
  205.  
  206.  
  207. /*** EOF util.c ***/
  208.